//
//  TKIMImageMsgView.m
//  TKIM
//
//  Created by liupm on 15/10/22.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMImageMsgView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "TKIMImageBrowserView.h"
#import "UIImageView+WebCache.h"
@interface TKIMImageMsgView()
@property(nonatomic,strong) UIImageView *thumbImageView;
@property(nonatomic,strong) TKIMImageMessage *imgMsg;
@property(nonatomic,strong) CAShapeLayer *maskLayer;
@end

@implementation TKIMImageMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [super drawView];
    [self.backgroundImageView addSubview:self.thumbImageView];
}


-(UIImageView *)thumbImageView
{
    if (!_thumbImageView)
    {
        _thumbImageView = [[UIImageView alloc] init];
    }
    return _thumbImageView;
}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    _imgMsg = (TKIMImageMessage *)message;
    
    //图片大小
    CGSize size = _imgMsg.imageSize;
    
    if(size.height == 0)
    {
        size = TKIMGetImage(@"im_icon_imageDefault").size;
    }
    
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width*0.4;
    if (size.width >maxWidth){
        size.height = maxWidth*size.height/size.width;
        size.width = maxWidth;
    }
    
    [self getMsgFrame:message size:size];

    //设置消息的大小
    self.thumbImageView.frame = CGRectMake(0, 0, self.backgroundImageView.width, self.backgroundImageView.height);

    //移除遮罩
    if(self.maskLayer.superlayer){
        [self.maskLayer removeFromSuperlayer];
    }
    //设置遮罩
    NSString *imageName = TKIMChatMsgReceiverImage;
    if (_imgMsg.isSender){
        imageName = TKIMChatMsgSenderImage;
    }
    UIImage *image =TKIMGetImage(imageName);
    self.maskLayer.contents = (id)image.CGImage;
    self.maskLayer.frame = (CGRect){ {0, 0}, self.backgroundImageView.frame.size };

    if(_imgMsg.thumbImage){
        self.thumbImageView.image =_imgMsg.thumbImage;
        self.thumbImageView.contentMode = UIViewContentModeScaleToFill;
        [self.thumbImageView.layer setMask:self.maskLayer];

    }else
    {
        self.thumbImageView.contentMode = UIViewContentModeCenter;
        TKIMWS(weakSelf)
        [self.thumbImageView sd_setImageWithURL:[NSURL URLWithString:_imgMsg.realThumbUrl] placeholderImage:TKIMGetImage(@"im_icon_imageDefault") options:SDWebImageRetryFailed  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
                 _imgMsg.thumbImage = image;
                if(!error)
                {
                    weakSelf.thumbImageView.contentMode = UIViewContentModeScaleToFill;
                    [weakSelf.thumbImageView.layer setMask:self.maskLayer];
                }
             }
         ];
        
        
    }
    
    //图片预览
    UITapGestureRecognizer *showPic = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPic:)];
    self.backgroundImageView.userInteractionEnabled = YES;
    [self.backgroundImageView addGestureRecognizer:showPic];
}


-(CALayer *)maskLayer
{
    if (!_maskLayer)
    {
        _maskLayer = [CAShapeLayer layer];
        _maskLayer.contentsCenter = CGRectMake(0.25, 0.7, 0.1, 0.1);
        _maskLayer.contentsScale = [UIScreen mainScreen].scale;//非常关键设置自动拉伸的效果且不变形
    }
    return _maskLayer;
}


/**
 *  @Author lpm, 15-10-27 16:10:34
 *
 *  @brief  显示图片大图
 *
 *  @param tap
 */
-(void)showPic:(UIGestureRecognizer *)tap
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    
    TKIMImageBrowserView * browseView = [[TKIMImageBrowserView alloc] initWithFrame:window.bounds contentView:window];
    browseView.orgImagUrl = self.imgMsg.realOriginUrl;
    browseView.orgImage = self.imgMsg.originImage;
    browseView.thubImage = self.imgMsg.thumbImage;
    browseView.thubImageUrl = self.imgMsg.realThumbUrl;
    if (self.imgMsg.realOriginUrl){
        browseView.orgImagUrl = self.imgMsg.realOriginUrl;
    }else if(self.imgMsg.realThumbUrl){
        browseView.orgImagUrl  = self.imgMsg.realThumbUrl;
    }
    [browseView show:self.thumbImageView];
}



/**
 *  @Author lpm, 15-11-05 09:11:46
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    TKIMImageMessage *imgMsg = (TKIMImageMessage *)msg;
    //图片大小
    CGSize size = imgMsg.imageSize;
    
    if(size.height == 0)
    {
        size = TKIMGetImage(@"im_icon_imageDefault").size;
    }
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width*0.4;
    
    if (size.width >maxWidth)
    {
        size.height = maxWidth*size.height/size.width;
        size.width = maxWidth;
    }
    return   size.height + TKIMChatMsgPadding+14;
}
@end
